/**
 * Created by Administrator on 2016/12/26.
 */
$(function(){
var oLis = $('#tab-first li');
    oLoadFun(`http://192.168.59.3:8082/console/shopping/findFirstMateriel?pageNum=1&pageSize=10`,0);
    oLis.click(function(){
        var iIndex = $(this).index();
        var oCategoryType = 0;
        oLis.removeClass('active').eq(iIndex).addClass('active');

        var oUrl = '';
        switch(iIndex){
            case 0:
                oUrl = `http://192.168.59.3:8082/console/shopping/findFirstMateriel?pageNum=1&pageSize=10`;
                oCategoryType = 0;
                break;
            case 1:
                oUrl = `http://192.168.59.3:8082/console/shopping/findFirstDevice?pageNum=1&pageSize=10`;
                oCategoryType = 1;
                break;
            case 2:
                oUrl = `http://192.168.59.3:8082/console/shopping/findFirstFacility?pageNum=1&pageSize=10`;
                oCategoryType = 2;
                break;
            default:
                console.log("错误");
        }
        $('#tab2Content1').html('');
        oLoadFun(oUrl,oCategoryType);


    });

function oLoadFun (oUrl,oCategoryType){
    $.ajax({
        type : 'get',
        url  : oUrl,
        success :function(data){
            var oLists = JSON.parse(data).data.list;
            var oHtml1 = '';
            var aNumbers = [];
            for(var i=0; i<oLists.length; i++){
                aNumbers[i] = oLists[i].id;

                oHtml1 += ` <div class="level-one-wrap">
                                <div class="line-wrap">
                                    <div class="types left">
                                            <label>
                                                <div class="level-one">
                                                    <input data-number=${aNumbers[i]} type="checkbox" class="checkBox">
                                                    <input data-number=${aNumbers[i]} data-flag=${true} type="text" class="trigon">
                                                    <span class="levelOne-btn">${oLists[i].name}</span>
                                                </div>
                                            </label>
                                        </div>
                                        <div class="addImg-wrap left">
                                            <div class="addImg left"><i class="icon icon-plus"></i> 添加图片</div>
                                            <div class="img-wrap left"><img src=${oLists[i].imgUrl} alt=""></div>
                                            <div class="img-del left mar-left-10">删除</div>
                                        </div>
                                    </div>
                            </div>`

            }
            $('#tab2Content1').append(oHtml1);
            var oHtml2 ='';

            $.each($('.trigon') , function (del){
                var aNums = [];
                var oThis = $(this);
                var oFlag = oThis.attr('data-flag');

                aNums[del] = 0;

                oThis.click(function(){
                    //console.log(oFlag);
                    oHtml2 = '';
                    $.ajax({
                        type : 'get',
                        url  : `http://192.168.59.3:8082/console/shopping/findBaseCategoryByCondition?parentId=${oThis.attr('data-number')}&level=3&categoryType=${oCategoryType}&pageNum=1&pageSize=10`,
                        success : function (data) {
                            var oLists2 =  JSON.parse(data).data.list;
                            //console.log(oLists2);
                            //console.log(oFlag);
                            oHtml2 += `<div class="input-bottom">
                                           <div class="line-wrap">
                                               <div class="content-wrap level-two-wrap">`;

                            if(oLists2 !==[] && oFlag ==true){
                                for(var j=0; j<oLists2.length; j++){
                                    oHtml2 += ` <div class="line-wrap">
                                                    <div class="types left">
                                                        <label>
                                                            <div class="level-two"><input type="checkbox" class="checkBox"><span class="line-turn"></span><span class="levelOne-btn">${oLists2[j].name}</span></div>
                                                        </label>
                                                    </div>
                                                    <div class="addImg-wrap left">
                                                        <div class="addImg left"><i class="icon icon-plus"></i> 添加图片</div>
                                                        <div class="img-wrap left"><img src=${oLists2[j].imgUrl} alt=""></div>
                                                        <div class="img-del left mar-left-10">删除</div>
                                                    </div>
                                                </div>`
                                }
                            }
                            oHtml2 += `
                                <div class="add-wrap">
                                    <div class="line-wrap">
                                        <div class="content-wrap">
                                            <div class="types">
                                                <label>
                                                    <div class="level-two"><span class="line-turn"></span><span class="addBtn-two levelOne-btn"><i class="icon icon-plus jia"></i> 添加子分类</span></div>
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>`;

                        }
                    });
                    oThis.attr('data-flag' , 'false');
                    $('#tab2Content1').children().eq(del).after().append(oHtml2);

                    var oInputBtms  =$('.input-bottom');

                    if(aNums[del] == 0){
                        oInputBtms.eq(del).css('display','block');
                        aNums[del] = 1;
                        oThis.css({
                            'border-bottom' : '0',
                            'border-top' : '6px solid #000',
                            'border-left' : '6px solid #fff',
                            'border-right' : '6px solid #fff',
                        });
                    }else if(aNums[del] == 1){
                        oInputBtms.eq(del).css('display','none');
                        aNums[del] = 0;
                        oThis.css({
                            'border-bottom' : '6px solid #fff',
                            'border-top' : '6px solid #fff',
                            'border-left' : '6px solid #000',
                            'border-right' : '0',
                        });
                    }
                });
            });
/*            $('.trigon').click(function(){

                $.ajax({
                    type : 'get',
                    url  : `http://192.168.59.3:8082/console/shopping/findBaseCategoryByCondition?parentId=${$(this).data-id}&level=2&categoryType=${oCategoryType}&pageNum=1&pageSize=10`,
                    success : function (data) {
                        var oLists2 =  JSON.parse(data).data.list;

                        oHtml2 += `<div class="input-bottom">
                                        <div class="line-wrap">
                                            <div class="content-wrap level-two-wrap">`;
                        for(var j=0; j<oLists2.length; j++){
                            oHtml2 += ` <div class="line-wrap">
                                            <div class="types left">
                                                <label>
                                                    <div class="level-two"><input type="checkbox" class="checkBox"><span class="line-turn"></span><span class="levelOne-btn">${oLists[i].name}</span></div>
                                                </label>
                                            </div>
                                            <div class="addImg-wrap left">
                                                <div class="addImg left"><i class="icon icon-plus"></i> 添加图片</div>
                                                <div class="img-wrap left"><img src=${oLists[i].imgUrl} alt=""></div>
                                                <div class="img-del left mar-left-10">删除</div>
                                            </div>
                                        </div>`
                        }
                        oHtml2 += `</div>
                                <div class="add-wrap">
                                    <div class="line-wrap">
                                        <div class="content-wrap">
                                            <div class="types">
                                                <label>
                                                    <div class="level-two"><span class="line-turn"></span><span class="addBtn-two levelOne-btn"><i class="icon icon-plus jia"></i> 添加子分类</span></div>
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>`
                    }
                })
            });*/
        }
    })
}


/*    $('#tab3').click(function () {
        $.ajax({
            type : 'get',
            url : `http://192.168.59.3:8082/console/shopping/findFirstFacility?pageNum=1&pageSize=10`,  //查看一级？
            //url : `http://192.168.59.3:8082/console/shopping/findBaseCategoryByCondition?parentId=1&level=1&categoryType=2&pageNum=1&pageSize=10`, //查看二级
            success : function (data) {
                var oLists = JSON.parse(data).data.list;
                var oLens31  = oLists.length;
                var oHtml3 = '';
                for(var j=0; j<oLens31; j++){
                oHtml3 += `<div class="line-wrap">
                                <div class="types left">
                                    <label>
                                        <div class="level-two"><input type="checkbox" class="checkBox"><span class="line-turn"></span><span class="levelOne-btn">${oLists[j].name}</span></div>
                                    </label>
                                </div>
                                <div class="addImg-wrap left">
                                    <div class="addImg left"><i class="icon icon-plus"></i> 添加图片</div>
                                    <div class="img-wrap left"><img src=${oLists[j].imgUrl} alt=""></div>
                                    <div class="img-del left mar-left-10">删除</div>
                                </div>
                            </div>`
                }
                var oLevelTwos = $('.level-two-wrap');
                var oLens32    = oLevelTwos.length;
                for(var k=0; k<oLens32; k++){
                    //console.log(oLevelTwos.eq(k));
                    oLevelTwos.eq(k).html(oHtml3);
                }

                var
                    oTrigons    = $('.trigon'),
                    oTriLens    = oTrigons.length,
                    oInputBtms  =$('.input-bottom');

                var iNum = 0;

                oTrigons.click(function(){
                    var iIndex = $(this).index()+1;
                    if(iNum == 0){
                        oInputBtms.eq(iIndex).css('display','block');
                        iNum = 1;
                    }else if(iNum == 1){
                        oInputBtms.eq(iIndex).css('display','none');
                        iNum = 0;
                    }
                });


                var oAddBtns = $('input[name=addBtn-two]');
                oAddBtns.on({
                    click:function(){
                        oAddBtns.removeAttr('readonly');
                    },
                    blur : function(){
                        if(oAddBtns.val() !== " 添加子分类"){
                            confirm('您确定要添加吗?');
                            if(confirm('您确定要添加吗?')){
                                $.ajax({
                                    type : 'post',
                                    url : `http://192.168.59.3:8082/console/shopping/saveBaseCategory?parentId=1&name=${oAddBtns.val()}&merchandiseImgQuantity=1`,
                                    success : function(data){
                                        console.log(data);
                                    }
                                })
                            }else{
                                return false;
                            }
                        }else{
                            return false;
                        }

                    }
                });
            }
        });
    });*/

$('#delete1').click(function(){
    var aIds = [];
    $.each($('input[type="checkbox"]') , function(){
       var aIs = $(this).attr('data-number');
        //console.log(aIs);
        //console.log($(this)[0].checked);
        //console.log(typeof $(this)[0].checked);
        if($(this)[0].checked){
            console.log(111);
            if(typeof aIs !== "undefined"){
                aIds += aIs + ',';
            }
        }
    });
    console.log(aIds);
if(aIds  !== []){
    $.ajax({
        type : 'delete',
        url : `http://192.168.59.3:8082/console/shopping/deleteBaseCategory?ids=${aIds}`,
        success : function(){
            console.log("成功");
        }
    })
}
});


    var oFlag2 = true;

    /*添加一级分类*/
    $('#addClassify1').click(function(){
        if(oFlag2){
            oFlag2 = false;
        var oHtmlAdd = `<div id="oHtmlAdd" class="line-wrap level-one-wrap">
                    <div class="types left">
                        <label>
                            <div class="level-one"><input type="checkbox" class="checkBox"><input type="text" class="trigon"><span id="add-input" class="levelOne-btn"><input type="text" value=""></span></div>
                        </label>
                    </div>
                    <div class="addImg-wrap left">
                        <div class="addImg left"><i class="icon icon-plus"></i> 添加图片</div>
                        <div class="img-wrap left"><img src="" alt=""></div>
                        <div id="add-save" class="img-save left mar-left-10">保存</div>
                        <div id="add-cancel" class="img-cancel left mar-left-10">取消</div>
                    </div>
                </div>`;
        $('#tab2Content1').append(oHtmlAdd);

        /*保存*/
        $('#add-save').click(function () {
            var oInputVal = $('#add-input input').val();
            console.log(oInputVal);
            $.ajax({
                type : 'post',
                url : `http://192.168.59.3:8082/console/shopping/saveBaseCategory?parentId=1&name=${oInputVal}&merchandiseImgQuantity=4&isleaf=0`,
                success : function(data){
                    console.log("成功" +  data);
                    $('#oHtmlAdd').remove();
                    var oAddData = JSON.parse(data).data;
                    var oHtmlAdd2 = `<div id="oHtmlAdd" class="line-wrap level-one-wrap">
                    <div class="types left">
                        <label>
                            <div class="level-one"><input type="checkbox" class="checkBox"><input type="text" class="trigon"><span id="add-input" class="levelOne-btn">${oAddData.name}</span></div>
                        </label>
                    </div>
                    <div class="addImg-wrap left">
                        <div class="addImg left"><i class="icon icon-plus"></i> 添加图片</div>
                        <div class="img-wrap left"><img src=${oAddData.imgUrl} alt=""></div>
                        <div class="img-del left mar-left-10">删除</div>
                    </div>
                </div>`;
                    $('#tab2Content1').append(oHtmlAdd2);
                }
            });
            oFlag2 = true;
        });
        /*取消*/
        $('#add-cancel').click(function () {
            $('#oHtmlAdd').remove();
            oFlag2 = true;
        });

        }
    });

    $("#allSec1 input").change(function () {
        if($("#allSec1 input").attr("checked")){
            console.log("全选");
            $('input[type="checkbox"]').attr("checked",false);
        }else{
            console.log("没有全选");
            $('input[type="checkbox"]').attr("checked",true);
        }
    });

});